package models;

import java.lang.reflect.Method;
import voidlib.*;
import models.mappers.*;


/**
 *
 * @author eliseev
 */
public class User extends VoidModel
{
    protected VoidMapper mapper = new UserMapper();
    
    @Override
    public VoidMapper getMapper() {
        return mapper;
    }
    
    @Override
    protected String _getModelId() {
        return "id";
    }     
    
    public User() {
    }
    
    public User(String login, String password, String title, int balance, UserRole role) {
        _login=login;
        _password = password;
        _title = title;
        _balance = balance;
        _role=role;
    }
    public UserRole _role;
    public int _id = -1;
    public String _login;
    public String _password;
    public String _title;
    public int _balance;
    
    public int getBalance() {
        return _balance;
    }
    
    public String getLogin() {
        return _login;
    }
    
    public String getTitle() {
        return _title;
    }
    
    
    public String getPassword() {
        return _password;
    }
    
    public UserRole getRole() {
        return _role;
    }
    
    public int getId() {
        return _id; 
    }
    
    public <T> T getSpecificUser() {
        UserRole role = getRole();
        Class<?> cl = VoidConfig.Role2Model.get(role);
        T res = null;
        try {
            Method method = cl.getMethod("getMapper");
            VoidMapper specificMapper = (VoidMapper)method.invoke(cl.newInstance());
            res = specificMapper.fetchOne("user_id = ?", getId());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return res;
    }
    
}
